package com.enjoyor.soa.traffic.rest.efoms.dubbo.invoke;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.enjoyor.soa.traffic.api.dto.vms.ProgInfoDto;
import com.enjoyor.soa.traffic.api.service.dubbo.vms.IDubboProgManageService;
import com.enjoyor.soa.traffic.util.helper.JsonHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Component
public class DubboLedProgInfoServiceInvoke {

	//@Reference(timeout = 10000)
	@Reference(timeout = 10000, url="dubbo://172.100.102.34:28803")
    private IDubboProgManageService dubboProgManageService ;

    /**
     * 获取诱导屏当前节目信息
     * @return
     */
    public ResultPojo getOnProgList(String ledId) {
    	ResultPojo resultPojo = new ResultPojo();
    	if (null != dubboProgManageService) {
    		ResultPojo pojo = dubboProgManageService.getOnProgList(ledId);
    		if(pojo != null && pojo.getAppCode() != null && pojo.getAppCode().compareTo("0") == 0 && pojo.getResultList() != null) {
    			Map<String, Object> ledProgMap = new HashMap<String, Object>();
    			JSONObject jsonpara = JSONObject.parseObject(JSONObject.toJSON(pojo.getResultList()).toString());
    			List<JSONObject> progInfoList = (List<JSONObject>)jsonpara.get("lstProgInfo");
    			List<ProgInfoDto> resultList = new ArrayList<ProgInfoDto>();
    			if(null != progInfoList || progInfoList.size() > 0) {
    				for(int i=0; i<progInfoList.size(); i++) {
    					ProgInfoDto progInfoDto = (ProgInfoDto)JsonHelper.jsonToObject(progInfoList.get(i).toString(), ProgInfoDto.class);
    					String picPath = progInfoDto.getPicPath();
        				if(null != picPath && !"".equals(picPath)) {
            				String progName = picPath.substring(picPath.indexOf("(")+1,picPath.lastIndexOf(")"));
            				progInfoDto.setProgName(progName);
        				}
        				resultList.add(progInfoDto);
    				}
    			}
    			resultPojo = ResultHelper.getResult(resultList);
    		}
    	}
        return resultPojo;
    }
    
    /**
     * 获取是否有当前节目
     * @return
     */
    public boolean getIsLedProgram(String ledId) {
    	//ledId = "20001";
    	boolean resultFlag = false;
    	if (null != dubboProgManageService) {
    		ResultPojo pojo = dubboProgManageService.getOnProgList(ledId);
    		if(pojo != null && pojo.getAppCode() != null && pojo.getAppCode().compareTo("0") == 0 && pojo.getResultList() != null) {
    			Map<String, Object> ledProgMap = new HashMap<String, Object>();
    			JSONObject jsonpara = JSONObject.parseObject(JSONObject.toJSON(pojo.getResultList()).toString());
    			List<JSONObject> progInfoList = (List<JSONObject>)jsonpara.get("lstProgInfo");
    			if(null != progInfoList && progInfoList.size() > 0) {
    				resultFlag = true;
    			}
    		}
    	}
        return resultFlag;
    }
    
    /**
     * 获取诱导屏总节目信息
     * @return
     */
    public ResultPojo getProgList(String ledId) {
    	ResultPojo pojo = new ResultPojo();
    	if (null != dubboProgManageService) {
    		pojo = dubboProgManageService.getProgList(ledId);
    	}
        return pojo;
    }
}
